package hn.cch.spring_boot_security_rest.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


@EnableSwagger2
@Configuration
public class SwaggerConfiguration {

    @Value("${swagger.enable:false}")
    private boolean swaggerEnable;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(swaggerEnable)//是否启动swagger
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage("hn.cch.spring_boot_security_rest.controller"))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(buildParam());
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("spring_boot_security_rest")
                .description("spring boot security_ est")
                .termsOfServiceUrl("http://localhost:9009/")
                .contact(new Contact("hangcchhn", "http://cch.hn", "hangcchhn@163.com"))
                .version("1.0")
                .build();
    }

    private List<Parameter> buildParam(){
        List<Parameter> parameterList = new ArrayList<>();

        ParameterBuilder cookieBuilder = new ParameterBuilder();
        cookieBuilder.name("Cookie").description("access token")
                .modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        parameterList.add(cookieBuilder.build());

        ParameterBuilder signBuilder = new ParameterBuilder();
        signBuilder.name("sign").description("签名")
                .modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        parameterList.add(signBuilder.build());
        return parameterList;
    }
}
